<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use \org\auth;
use app\common\controller\AdminBaseController;

class Users extends Controller
{
	protected function _initialize() {
        parent::_initialize();
        $store_menu_list       = Db::name('tg_auth_rule') ->order(['sort' => 'ASC'])->select();
        $store_menu_level_list = array2level($store_menu_list);
        $this->assign('store_menu_level_list', $store_menu_level_list);
        
    }
    
    //管理员列表
    public function admin_list(){
        $data=Db::name('tg_auth_user')->select();
        $this->assign('data',$data);
        return  $this->fetch();
    }

    //添加管理员
    public function admin_add(){
    	if($_POST){
            $data['admin_name']=$_POST['admin_name'];
            $data['status']=$_POST['status'];
            $data['real_name']=$_POST['real_name'];
            $data['admin_password']=md5_salt($_POST['admin_password']);
            $group['group_id']=$_POST['group_id'];
            
                $res1=Db::name('tg_auth_user')->insertGetId($data);//用户数据库
                $group['uid']=$res1;
                $res2=Db::name('tg_auth_group_access')->insert($group);//分组数据库
                if($res1 && $res2){
                    return	toSuccess('添加成功');
                } else {
                   return toError(408,'添加失败');
                }
            
        }
        $data=Db::name('tg_auth_group')->where("status=1")->select();
        $this->assign('data',$data);
        return  $this->fetch();
    }
    
    //修改管理员界面
	public function admin_edit()
    {	
    	$id=input('id');
    	
        if($_POST){
        	$data['id']=$_POST['id'];
        	$data['admin_name']=$_POST['admin_name'];
            $data['status']=$_POST['status'];
			if(input("op_pwd")){
				if(input("op_pwd")!=input("op_pwd1")){
					return toError(408,'操作密码不一致');
				}else{
					$data['op_pwd']=input("op_pwd");
				}
			}
            $data['real_name']=$_POST['real_name'];
			if(input("admin_password")){
				$data['admin_password']=md5_salt($_POST['admin_password']);
			}
            $group['group_id']=$_POST['group_id'];
            if($id == 1){
            	unset($data['id']);
            	 $res1=Db::name('tg_auth_user')->where('id',$id)->update($data);//用户数据库
            	 if($res1){
                	return	toSuccess('修改成功');
                }else{
                   return toError(408,'修改失败');
                } 
            }
            if(Db::name('tg_auth_group_access')->where('uid',$id)->find()){
                $res1=Db::name('tg_auth_user')->where('id',$id)->update($data);//用户数据库
                $res2=Db::name('tg_auth_group_access')->where('uid',$id)->update($group);//分组数据库
             }else{
            	Db::name('tg_auth_group_access')->insert(['uid'=>$id,'group_id'=>$group['group_id']]);
            }
                if($res1 == 1  || $res2 == 1 ){
                	return	toSuccess('添加成功');
                }else{
                   return toError(408,'添加失败');
                } 
        	
        }
        $group_id=Db::name('tg_auth_group_access')->where('uid',$id)->find();
        $this->assign('list',Db::name('tg_auth_group')->where("status=1")->select());
        $this->assign('group_id',$group_id['group_id']);
        $this->assign('user_list',Db::name('tg_auth_user')->where('id',$id)->find());
        return $this->fetch();
    }
    
    //管理员删除
    public function admin_list_del(){
	   $data['id']=$_GET['id'];
       $result =Db::name('tg_auth_user')->where('id', $data['id'])->delete();;
        if($result){
		 return json(['code'=>200,'msg'=>'成功']);
       }else{
       		return json(['code'=>402,'msg'=>'失败']);
       }
    }
    
    //管理员停用
    public function admin_state(){
    	$id=input('id');
    	$status['status']=input('status_id');
        $data=Db::name('tg_auth_user')->where('id',$id)->update($status);
        if($data){
       		return json(['code'=>200,'msg'=>'成功']);
        }else{
        	return json(['code'=>402,'msg'=>'失败']);
        }
    }
    
    /*以上是管理员列表操作*/
     /*以下是权限节点*/
    
    //权限列表
    public function admin_permission(){
        $data=Db::name('tg_auth_rule')->where("status=1")->select();
        $this->assign('data',$data);
    	$this->assign('countt',Db::name('tg_auth_rule')->count());
        return  $this->fetch();
    }

    //添加权限
    public function admin_permission_add(){
    	if($_POST){
    	 	$data['pid']=$_POST['pid'];	
	        $data['name']=$_POST['name'];
	        $data['title']=$_POST['title'];
	        $data['type']=1;
	        $data['icon']=$_POST['icon'];
	        $data['sort']=$_POST['sort'];
	        $data['status']=$_POST['status'];
	        $data['add_time']=now_time();
	        $m=Db::name('tg_auth_rule');
	        $result=$m->insert($data);
	        	if($result){
				 return json(['code'=>200,'msg'=>'成功']);
		       }else{
		       	 return json(['code'=>402,'msg'=>'失败']);
		       }
    	}
        return $this->fetch();
    }
    
    
    //修改权限到数据库
    public function admin_permission_edit(){
    	if($_POST){
    		$id=$_POST['id'];
    		$data['pid']=$_POST['pid'];	
	        $data['name']=$_POST['name'];
	        $data['title']=$_POST['title'];
	        $data['type']=1;
	        $data['icon']=$_POST['icon'];
	        $data['sort']=$_POST['sort'];
	        $data['status']=$_POST['status'];
	        $result=Db::name('tg_auth_rule')->where('id',$id)->update($data);
	        if($result){
				return json(['code'=>200,'msg'=>'成功']);
	       }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	       }
    	}
    	
    	$id=input('id');
        $data=Db::name('tg_auth_rule')->where('id',$id)->find();
        $this->assign('data',$data);
        $this->assign('menu',Db::name('tg_auth_rule')->find($id));
    	
        return  $this->fetch();
    }
    
	//删除权限到数据库
	public function admin_permission_del(){
        $data['id']=$_GET['id'];
       $result =Db::name('tg_auth_rule')->where('id', $data['id'])->delete();;
        if($result){
			 return json(['code'=>200,'msg'=>'成功']);
       }else{
       		return json(['code'=>402,'msg'=>'失败']);
       }
       
    }
    //删除多条权限到数据库
	public function admin_permission_ddel(){
        $newstr=$_GET['newstr'];
       $result =Db::name('tg_auth_rule')->where('id in ('.$newstr.')')->delete();;
        if($result){
			return json(['code'=>200,'msg'=>'成功']);
       }else{
       		return json(['code'=>402,'msg'=>'失败']);
       }
       
    }
	//权限节点停用
    public function permission_state(){
    	$id=input('id');
    	$status['status']=input('status_id');
        $data=Db::name('tg_auth_rule')->where('id',$id)->update($status);
        if($data){
       		return json(['code'=>200,'msg'=>'成功']);
        }else{
        	return json(['code'=>402,'msg'=>'失败']);
        }
    }
    /*以上是权限节点*/
    
   
    /*以下角色管理*/
    
    //角色列表
    public function admin_role(){
    	$data =Db::name('tg_auth_group')->select();
	    $this->assign('data',$data);
        return  $this->fetch();
    }
    //角色删除
    public function admin_role_del(){
    	   $data['id']=$_GET['id'];
	       $result =Db::name('tg_auth_group')->where('id', $data['id'])->delete();;
	        if($result){
				return json(['code'=>200,'msg'=>'成功']);
	       }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	       }
    }
    //添加角色
    public function admin_role_add(){
        if($_POST){
	        $data['title']=$_POST['title'];
	        $data['status']=$_POST['status'];
	        $result=Db::name('tg_auth_group')->insert($data);
	        if($result){
				return json(['code'=>200,'msg'=>'成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	        }
        }
      
        return $this->fetch();
    }
    
    //角色修改
    public function admin_role_edit(){
    	   
	       if($_POST){
	       	$id=input('id');
	        $data['title']=$_POST['title'];
	        $data['status']=$_POST['status'];
	        $result=Db::name('tg_auth_group')->where('id', $id)->update($data);
	        if($result){
				return json(['code'=>200,'msg'=>'成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	        }
        }
       $id=input('id');
       $data =Db::name('tg_auth_group')->where('id', $id)->find();
       $this->assign('data',$data);
       return  $this->fetch();
    }

    
    /*以上是角色管理*/
    
    
    
    //分配权限查询
	public function role_edit($id){
		$auth_group_data = Db::name('tg_auth_group')->field('rules')->where('id',$id)->find();   	
			$s2=implode(',',$auth_group_data);
			$auth_rules      = explode(',', $s2);
			$auth_rule_list  = Db::name('tg_auth_rule')->field('id,pid,title')->select();
			foreach ($auth_rule_list as $key => $value) {
         		in_array($value['id'], $auth_rules) && $auth_rule_list[$key]['checked'] = true;
    }
		return json(['code' => 200, 'data' => json_encode($auth_rule_list), 'msg' => 'success','r_id'=>$id]);
	}
    //分配权限提交
    public function role_edit_ok($id){
		
		if(Db::name('tg_auth_group')->where('id',input('id'))->update(['rules'=>input('rules')])){
			return json(['code' => 200, 'msg' => '授权成功']);
		}else{
			return json(['code' => 408, 'msg' => '授权失败']);
			
		}
	}
    
}
